package com.mashibing.test.case23.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class CalculatorProxy {
	public static Calculator getProxy(final Calculator calculator){
		ClassLoader loader = calculator.getClass().getClassLoader();
		Class<?>[] interfaces = calculator.getClass().getInterfaces();
		InvocationHandler h = new InvocationHandler() {
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				Object result = null;
				try {
					System.out.println("------------------before------------------");
					result = method.invoke(calculator, args);
					System.out.println("method:" + method.getName());
					System.out.println("args :"+ Arrays.asList(args));
					System.out.println("result :"+ result);
					System.out.println("------------------after------------------");
				} catch (Exception e) {
				} finally {
				}
				return result;
			}
		};
		Object proxy = Proxy.newProxyInstance(loader, interfaces, h);
		return (Calculator) proxy;
	}
}
